import { useUSerStore } from '@/stores'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { createRouter, createWebHistory } from 'vue-router'

// 初始化路由实例
const router = createRouter({
  // createWebHistory是history路由
  // createWebHashHistory 是hash路由
  // import.meta.env.BASE_URL 是根路径
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      meta: { title: '登录' },
      // 使用路由懒加载是用的import (''),第二个写的是conponent
      component: () => {
        return import('@/views/Login/index.vue')
      }
    },
    {
      path: '/order/:id',
      component: () => import('@/views/order/OrderDetail.vue'),
      meta: { title: '药品订单详情' }
    },
    {
      path: '/order/logistics/:id',
      component: () => import('@/views/order/OrderLogistics.vue'),
      meta: { title: '物流详情' }
    },
    {
      path: '/order/pay/result',
      component: () => import('@/views/order/OrderPayResult.vue'),
      meta: { title: '药品支付结果' }
    },
    {
      path: '/order/pay/:id',
      component: () => import('@/views/order/orderPay.vue'),
      meta: { title: '药品支付' }
    },
    {
      path: '/user/consult',
      component: () => import('@/views/user/consultPage.vue'),
      meta: { title: '问诊记录' }
    },
    {
      path: '/user/consult/:id',
      component: () => import('@/views/user/ConsultDetail.vue'),
      meta: { title: '问诊详情' }
    },
    {
      path: '/room',
      component: () => import('@/views/Room/index.vue'),
      meta: { title: '问诊室' },
      beforeEnter(to) {
        if (to.query.payResult === 'false') return 'user/consult'
      }
    },
    {
      path: '/consult/illness',
      component: () => {
        return import('@/views/Consult/ConsultIllness.vue')
      },
      meta: { title: '病情描述' }
    },
    {
      path: '/consult/Pay',
      component: () => {
        return import('@/views/Consult/ConsultPay.vue')
      },
      meta: { title: '问诊支付' }
    },
    {
      path: '/login/callback',
      component: () => import('@/views/Login/LoginCallback.vue'),
      meta: { title: 'QQ登录-绑定手机' }
    },
    {
      path: '/user/patient',
      component: () => import('@/views/user/PatientPage.vue'),
      meta: { title: '家庭档案' }
    },
    {
      path: '/consult/dep',
      component: () => import('@/views/Consult/ConsultDep.vue'),
      meta: { title: '选择科室' }
    },
    {
      path: '/consult/fast',
      component: () => import('@/views/Consult/ConsultFast.vue'),
      meta: { title: '极速问诊' }
    },
    {
      path: '/',
      redirect: '/home',
      component: () => {
        return import('@/views/layout/index.vue')
      },
      children: [
        {
          path: '/home',
          meta: { title: '首页' },
          component: () => {
            // component组件是一个函数,在配置的时候要给返回值
            return import('@/views/home/index.vue')
          }
        },
        {
          path: '/article',
          meta: { title: '健康百科' },
          component: () => {
            return import('@/views/article/index.vue')
          }
        },
        {
          path: '/notify',
          meta: { title: '消息中心' },
          component: () => {
            return import('@/views/notify/index.vue')
          }
        },
        {
          path: '/user',
          meta: { title: '我的' },
          component: () => {
            return import('@/views/user/index.vue')
          }
        }
      ]
    }
  ]
})

// 配置全局前置导航守卫
router.beforeEach((to) => {
  NProgress.start()
  // 取出token
  const store = useUSerStore()
  const WhiteList = ['/login', '/login/callback']

  // to.path是获取到当前的路径
  if (!store.user?.token && !WhiteList.includes(to.path)) {
    return '/login'
  }
})

//配置全局后置导航守卫
router.afterEach((to) => {
  //在网页中获取titile信息
  document.title = `${to.meta.title || ''}-优医问诊`
  NProgress.done()
})
NProgress.configure({
  showSpinner: false
})
export default router
